package com.bl.weilan.service.biz.user.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.ListUtils;
import com.bl.weilan.dao.model.*;
import com.bl.weilan.dao.redis.UserInfoDao;
import com.bl.weilan.dao.redis.model.UserInfo;
import com.bl.weilan.dao.repository.*;
import com.bl.weilan.service.biz.user.UserService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.user.*;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yujingyi on 2018/1/15.
 */
@Service
@Transactional
@Log4j2
public class UserServiceImpl implements UserService {

    final static private int VALID_INVITED_REWARDED_SALIVA = 20;

    @Autowired
    UserRepository userRepository;

    @Autowired
    UserInfoDao userInfoDao;


    @Override
    public EmployeeEntity getUserById(String userId) {
        EmployeeEntity userEntity = userRepository.findOne(userId);
        return userEntity;
    }

    @Override
    public EmployeeEntity getUserByMobile(String mobile) {
        List<EmployeeEntity> userEntityList = userRepository.findByMobile(mobile);
        return ListUtils.getFirst(userEntityList);
    }

    @Override
    @Transactional
    public void createUser(EmployeeEntity userEntity) {
        userRepository.save(userEntity);
    }

    @Override
    @Transactional
    public void modifyUser(UserInfoModifyVo userInfoModifyVo, UserInfo userInfo) {
        EmployeeEntity userEntity = userRepository.findOne(userInfo.getId());
        if (userEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST);
        }
        userEntity.setAvatar(userInfoModifyVo.getAvatar());

        userRepository.save(userEntity);

        // if has login, update redis
//        boolean perfect = Strings.isNullOrEmpty(userEntity.getName()) ? false : true;
        Optional<UserInfo> userInfoOptional = userInfoDao.getUserInfo(userEntity.getId());
        if (userInfoOptional.isPresent()) {
//            userInfoDao.setUserInfoAttribute(userEntity.getMobile(), "perfect", String.valueOf(perfect));
        }
    }

    @Override
    public EmployeeEntity getUserByInviteCode(String inviteCode) {
        return null;
    }


    @Override
    public List<TagEntity> getUserTag(String userId) {
        return null;
    }

    @Override
    public UserListVo getUserListMulti(String id, String mobile, String nickname, String status, Pageable pageable) {
        Page <EmployeeEntity> userEntities;
        List<UserInfoVo> userInfoVoList = new ArrayList<>();
        UserListVo userListVo = new UserListVo();

        if(id == null && mobile == null && nickname == null && status == null) {
            userEntities = userRepository.findAll(pageable );
        } else {
            userEntities = userRepository.findMultList(id, mobile, nickname, status, pageable );
        }

        for (EmployeeEntity userEntity : userEntities) {
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setStatus(userEntity.getStatus());
            userInfoVo.setMobile(userEntity.getMobile());
            userInfoVo.setAvatar(userEntity.getAvatar());
            userInfoVo.setCreateTime(userEntity.getCreateTime());
            userInfoVo.setId(userEntity.getId());
            userInfoVo.setCity(userEntity.getCity());
            userInfoVo.setInstitution(userEntity.getInstitution());
            userInfoVo.setMobile(userEntity.getMobile());
            userInfoVo.setUpdateTime(userEntity.getUpdateTime());
            userInfoVo.setVip(userEntity.getVip());
            userInfoVo.setContacter(userEntity.getContacter());


            userInfoVoList.add(userInfoVo);

        }
        userListVo.setTotalCount(userEntities.getTotalElements());
        userListVo.setList(userInfoVoList);

        return userListVo;
    }

    @Override
    public void del(DelBody id) {
        userRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            userRepository.delete(id);
        }
    }

    @Override
    public void modify(UserInfoManageModifyVo userInfoModifyVo) {
        EmployeeEntity userEntity = userRepository.findOne(userInfoModifyVo.getId());
        if (userEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST);
        }
        userEntity.setName(userInfoModifyVo.getName());
        userEntity.setVip(userInfoModifyVo.getVip());
        userEntity.setAvatar(userInfoModifyVo.getAvatar());
        userEntity.setInstitution(userInfoModifyVo.getInstitution());
        userEntity.setCity(userInfoModifyVo.getCity());
        userEntity.setAvatar(userInfoModifyVo.getAvatar());
        userEntity.setStatus(userInfoModifyVo.getStatus());
        userEntity.setContacter(userInfoModifyVo.getContacter());
        userRepository.save(userEntity);

    }


}
